<?php

/**
 * member actions.
 *
 * @package    cpfaizenay
 * @subpackage news
 * @author     Manu
 * @version    1.0
 */
class memberActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request) {
      $slug = $request->getParameter('name_slug');
      if(empty($slug)){
            if(!$this->getUser()->isAuthenticated()){
                $this->redirect('@localized_homepage');
            }
            $user = $this->getUser()->getGuardUser()->getProfile();
            $slug = Utils::slugify($user->getLastName() . " " . $user->getFirstName());
      }

      $this->member_info = Doctrine::getTable('Licensee')->getMemberInfo($slug)->fetchOne();
      $this->forward404Unless($this->member_info);
      
      $this->licensee_point_list = Doctrine::getTable('LicenseePoint')->getLicenseePoint($this->member_info['id'], null)->fetchArray();
      $this->licensee_match_list = Doctrine::getTable('LicenseeMatch')->getLicenseeMatch($this->member_info['id'])->fetchArray();
      $this->scoring_table = Doctrine::getTable('ScoringTable')->getScoringTable()->fetchArray();

      isicsBreadcrumbs::getInstance()->addItem('Profil de ' . $this->member_info[ 'lastname' ] . " " . $this->member_info[ 'firstname' ], '#');
      $response = $this->getResponse();
      Utils::setSlogTitle('Profil de ' . $this->member_info[ 'lastname' ] . " " . $this->member_info[ 'firstname' ], $response);
  }

  public function executeEvolution(sfWebRequest $request) {
      $slug = $request->getParameter('name_slug');
      $this->seasonStart = $request->getParameter('seasonStart');

      if(empty($this->seasonStart)){
            $this->seasonStart = date('m') > 6 ? date('Y') : (date('Y')-1);
      }

      if(empty($slug)){
            if(!$this->getUser()->isAuthenticated()){
                $this->redirect('@localized_homepage');
            }
            $user = $this->getUser()->getGuardUser()->getProfile();
            $slug = Utils::slugify($user->getLastName() . " " . $user->getFirstName());
      }

      $this->member_info = Doctrine::getTable('Licensee')->getMemberInfo($slug)->fetchOne();
      $this->forward404Unless($this->member_info);

      $this->season_list = Doctrine::getTable('LicenseePoint')->getLicenseePointSeasonAvailable($this->member_info['id'])->fetchArray();
      $this->season_list = $this->season_list[0]; // Bof bof
      
      $this->licensee_point_list = Doctrine::getTable('LicenseePoint')->getLicenseePoint($this->member_info['id'], $this->seasonStart)->fetchArray();

      isicsBreadcrumbs::getInstance()->addItem('Evolution de ' . $this->member_info[ 'lastname' ] . " " . $this->member_info[ 'firstname' ], '#');
      $response = $this->getResponse();
      Utils::setSlogTitle('Evolution de ' . $this->member_info[ 'lastname' ] . " " . $this->member_info[ 'firstname' ], $response);
  }

  public function executeMatchList(sfWebRequest $request) {
      $slug = $request->getParameter('name_slug');
      $this->seasonStart = $request->getParameter('seasonStart');

      if(empty($this->seasonStart)){
            $this->seasonStart = date('m') > 6 ? date('Y') : (date('Y')-1);
      }

      if(empty($slug)){
            if(!$this->getUser()->isAuthenticated()){
                $this->redirect('@localized_homepage');
            }
            $user = $this->getUser()->getGuardUser()->getProfile();
            $slug = Utils::slugify($user->getLastName() . " " . $user->getFirstName());
      }
      
      $this->member_info = Doctrine::getTable('Licensee')->getMemberInfo($slug)->fetchOne();
      $this->forward404Unless($this->member_info);

      $this->season_list = Doctrine::getTable('LicenseePoint')->getLicenseePointSeasonAvailable($this->member_info['id'])->fetchArray();
      $this->season_list = $this->season_list[0]; // Bof bof

      $this->licensee_point_list = Doctrine::getTable('LicenseePoint')->getLicenseePoint($this->member_info['id'], $this->seasonStart)->fetchArray();

      $this->pager = new sfDoctrinePager(
        'LicenseeMatch',
        sfConfig::get('app_matchList_max_matchs')
      );
      $this->pager->setQuery(Doctrine::getTable('LicenseeMatch')->getLicenseeMatch($this->member_info['id'], $this->seasonStart));
      $this->pager->setPage($request->getParameter('page', 1));
      $this->pager->init();
      $this->licensee_match_list = $this->pager;

      $this->nbVictory = Doctrine::getTable('LicenseeMatch')->getNbVictory($this->member_info['id'], $this->seasonStart)->fetchOne();
      $this->nbVictory = $this->nbVictory['nb_victory'];

      $this->scoring_table = Doctrine::getTable('ScoringTable')->getScoringTable()->fetchArray();

      $this->form = new LicenseeMatchForm();

      isicsBreadcrumbs::getInstance()->addItem('Matchs joués de ' . $this->member_info[ 'lastname' ] . " " . $this->member_info[ 'firstname' ], '#');

      $response = $this->getResponse();
      Utils::setSlogTitle('Matchs joués de ' . $this->member_info[ 'lastname' ] . " " . $this->member_info[ 'firstname' ], $response);
  }

  public function executeEditMatchList(sfWebRequest $request){
      $id_licensee_match = $request->getParameter('id');
      $this->forward404Unless($id_licensee_match);

      $id_user = $this->getUser()->getGuardUser()->getId();
      $this->member_info = Doctrine::getTable('Licensee')->getMemberUserInfo($id_user)->fetchOne();
      $this->forward404Unless($this->member_info);

      $licensee_match = Doctrine::getTable('LicenseeMatch')->getCurrentLicenseeMatch($id_licensee_match, $this->member_info['id'])->fetchOne();

      $this->form = new EditLicenseeMatchForm($licensee_match);

      isicsBreadcrumbs::getInstance()->addItem('Matchs joués de ' . $this->member_info[ 'lastname' ] . " " . $this->member_info[ 'firstname' ], '@member_matchList?name_slug=' . $this->member_info[ 'slug' ]);
      isicsBreadcrumbs::getInstance()->addItem('Modifier ce match', '#');

      $response = $this->getResponse();
      Utils::setSlogTitle('Modifier ce match', $response);

      if ($request->isMethod('post')) {
          $values = $request->getParameter($this->form->getName());

          $this->form->bind($values);
          if($this->form->isValid()){
              $this->form->save();

              $currentSeason = date('Y', strtotime($this->form->getValue('datetime_match')));
              LicenseePoint::resetPoint($this->member_info['id'], $id_user, $currentSeason);
              LicenseePoint::doCalculPoints($this->member_info['id'], $id_user);

              $this->redirect('@member_myMatchList');
          }
      }
  }

  public function executeDeleteMatchList(sfWebRequest $request){
    $this->id_licensee_match = $request->getParameter('id');
    $this->forward404Unless($this->id_licensee_match);

    $id_user = $this->getUser()->getGuardUser()->getId();
    $this->member_info = Doctrine::getTable('Licensee')->getMemberUserInfo($id_user)->fetchOne();
    $this->forward404Unless($this->member_info);

    isicsBreadcrumbs::getInstance()->addItem('Matchs joués de ' . $this->member_info[ 'lastname' ] . " " . $this->member_info[ 'firstname' ], '@member_matchList?name_slug=' . $this->member_info[ 'slug' ]);
    isicsBreadcrumbs::getInstance()->addItem('Supprimer ce match', '#');

    $response = $this->getResponse();
    Utils::setSlogTitle('Supprimer ce match', $response);
  }

  public function executeDoDeleteMatchList(sfWebRequest $request){
    
    $id_licensee_match = $request->getParameter('id');
    $this->forward404Unless($id_licensee_match);

    $id_user = $this->getUser()->getGuardUser()->getId();
    $this->member_info = Doctrine::getTable('Licensee')->getMemberUserInfo($id_user)->fetchOne();
    $this->forward404Unless($this->member_info);

    $licensee_match = Doctrine::getTable('LicenseeMatch')->getCurrentLicenseeMatch($id_licensee_match, $this->member_info['id'])->fetchOne();
    $licensee_match->delete();

    $currentSeason = date('Y', strtotime($licensee_match['datetime_match']));
    LicenseePoint::resetPoint($this->member_info['id'], $id_user, $currentSeason);
    LicenseePoint::doCalculPoints($this->member_info['id'], $id_user);

    $this->redirect('@member_myMatchList');
  }

  public function executeSaveMatchList(sfWebRequest $request){
      $id_user = $this->getUser()->getGuardUser()->getId();
      $this->member_info = Doctrine::getTable('Licensee')->getMemberUserInfo($id_user)->fetchOne();
      $this->licensee_point_list = Doctrine::getTable('LicenseePoint')->getLicenseeUserPoint($id_user, null)->fetchArray();
      $this->forward404Unless($this->member_info);

      $this->form = new PointsCalculationForm();

      isicsBreadcrumbs::getInstance()->addItem('Matchs joués de ' . $this->member_info[ 'lastname' ] . " " . $this->member_info[ 'firstname' ], '@member_matchList?name_slug=' . $this->member_info[ 'slug' ]);
      isicsBreadcrumbs::getInstance()->addItem('Enregistrer vos matchs', '#');
      $response = $this->getResponse();
      Utils::setSlogTitle('Enregistrer vos matchs', $response);

      if ($request->isMethod('post')) {
          $values = $request->getParameter($this->form->getName());

          $this->form->bind($values);
          if($this->form->isValid()){
              $currentSeason = strtotime($this->form->getValue('datetime_match'));
              $currentSeason = date('m', $currentSeason) > 6 ? date('Y', $currentSeason) : (date('Y', $currentSeason)-1);
              
              LicenseePoint::resetPoint($this->member_info['id'], $id_user, $currentSeason);
              $this->form->save();
              LicenseePoint::doCalculPoints($this->member_info['id'], $id_user, $currentSeason);

              $this->redirect('@member_myMatchList');
          }
      }
  }

  public function executeEditAvatar(sfWebRequest $request) {
      $this->form = new AvatarForm();

      isicsBreadcrumbs::getInstance()->addItem('Mon profil', '@member_myProfil');
      isicsBreadcrumbs::getInstance()->addItem('Changer mon avatar', '#');
      $response = $this->getResponse();
      Utils::setSlogTitle('Changer mon avatar', $response);

      if ($request->isMethod('post')) {
          $values = $request->getParameter($this->form->getName());

          $this->form->bind($values, $request->getFiles($this->form->getName()));
          if($this->form->isValid()){
              $this->form->save();

              $this->redirect('@member_avatar');
          }
      }
  }

  public function executeEditUsername(sfWebRequest $request) {
      $this->form = new EditUserNameForm();

      isicsBreadcrumbs::getInstance()->addItem('Mon profil', '@member_myProfil');
      isicsBreadcrumbs::getInstance()->addItem('Changer mon pseudo', '#');
      $response = $this->getResponse();
      Utils::setSlogTitle('Changer mon pseudo', $response);

      if ($request->isMethod('post')) {
          $values = $request->getParameter($this->form->getName());

          $this->form->bind($values, $request->getFiles($this->form->getName()));
          if($this->form->isValid()){
              $this->form->save();

              $this->redirect('@member_change_username_finish');
          }
      }
  }

  public function executeEditUsernameFinish(sfWebRequest $request){
      isicsBreadcrumbs::getInstance()->addItem('Mon profil', '@member_myProfil');
      isicsBreadcrumbs::getInstance()->addItem('Changer mon pseudo', '#');
      $response = $this->getResponse();
      Utils::setSlogTitle('Changer mon pseudo', $response);
  }

  public function executeDeleteAccount(sfWebRequest $request) {
      isicsBreadcrumbs::getInstance()->addItem('Mon profil', '@member_myProfil');
      isicsBreadcrumbs::getInstance()->addItem('Désinscription', '#');
      $response = $this->getResponse();
      Utils::setSlogTitle('Désinscription', $response);
  }

  public function executeDeleteAccountConfirm(sfWebRequest $request) {
      $this->form = new DeleteAccountForm();

      isicsBreadcrumbs::getInstance()->addItem('Mon profil', '@member_myProfil');
      isicsBreadcrumbs::getInstance()->addItem('Confirmer désinscription', '#');
      $response = $this->getResponse();
      Utils::setSlogTitle('Confirmer désinscription', $response);

      if ($request->isMethod('post')) {
          $values = $request->getParameter($this->form->getName());

          $this->form->bind($values);
          if($this->form->isValid()){
              if($this->form->deleteConfirm()){
                $this->redirect('@sf_guard_signout');
              }
          }
      }
  }
}